#include "QThumbnailList.h"
#include <QVBoxLayout>
#include "VisionMainWnd.h"

QThumbnailList::QThumbnailList(QWidget* parent) : QWidget(parent)
{
    listWidget = new QListWidget(this);
    listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    listWidget->setFlow(QListView::LeftToRight);
    listWidget->setWrapping(false);
    listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    listWidget->setFixedHeight(100);
    listWidget->setViewMode(QListView::IconMode);
    listWidget->setIconSize(QSize(100, 100)); // 设置图标的大小
    listWidget->setResizeMode(QListView::Adjust);
    listWidget->setGridSize(QSize(100, 100)); // 设置每个项的大小
    listWidget->setMovement(QListView::Static);
    //listWidget->setMinimumWidth(1080);
    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(listWidget);
    setLayout(hbox);
    
    label_ = new QLabel();
    
    label_->setFixedWidth(1280);
    label_->setFixedHeight(720);
    label_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    label_->setAlignment(Qt::AlignCenter);
    //label_->setStyleSheet("border: 1px solid black;");
    //label_->setWindowFlag(Qt::WindowStaysOnTopHint);
    
    connect(listWidget, &QListWidget::itemClicked, this, &QThumbnailList::showFullSizeImage);
}

QThumbnailList::~QThumbnailList()
{
   /* if (label_->isActiveWindow())
    {
        delete label_;
    }*/
}

void QThumbnailList::addMatImageItem(cv::Mat& img, std::string str)
{
    QImage imgItem((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_BGR888);
    QPixmap iconImage = QPixmap::fromImage(imgItem);
    QListWidgetItem* item = new QListWidgetItem();
    item->setIcon(QIcon(iconImage));
    listWidget->addItem(item);
    item->setData(Qt::UserRole, str.c_str());
    if (listWidget->count() > maxCount_)
    {
        auto headItem = listWidget->takeItem(0);
        listWidget->removeItemWidget(headItem);
        if (headItem)
        {
            delete headItem;
            headItem = nullptr;
        }
    }

    cv::Mat saveImg = img.clone();
    iconMatList_.push_back(saveImg);
    if (iconMatList_.size() > maxCount_)
    {
        iconMatList_.pop_front();
    }
}

void QThumbnailList::addMatItem(cv::Mat& item, QString imgPath)
{
    QImage img((uchar*)item.data, item.cols, item.rows, item.step, QImage::Format_BGR888);
    addImageItem(img, imgPath);    
}

#include <QDateTime>

void QThumbnailList::addImageItem(QImage &img, QString imgPath)
{
    iconImageList_.push_back(img);

    QPixmap iconImage = QPixmap::fromImage(img);
    QListWidgetItem* item = new QListWidgetItem();
    item->setIcon(QIcon(iconImage));
    listWidget->addItem(item);
    item->setData(Qt::UserRole, imgPath);
    // 大于指定数目，删除第一项
    if (listWidget->count() > maxCount_)
    {
        iconImageList_.pop_front();

        auto headItem = listWidget->takeItem(0);
        listWidget->removeItemWidget(headItem);
        if (headItem)
        {
            delete headItem;
            headItem = nullptr;
        }
    }

    // 滚动到最新项
    listWidget->addItem(item);
    listWidget->scrollToItem(listWidget->item(listWidget->count() - 1), QAbstractItemView::PositionAtBottom);
}

void QThumbnailList::addImageItems(QStringList& files)
{
    setIconMaxNums(files.size());
    
    QStringList imageFiles = files;


    for (int i = 0; i < imageFiles.size(); ++i)
    {
        QImage temp;
        temp.load(imageFiles[i]);
        iconImageList_.push_back(temp);

        QPixmap nailImg(imageFiles[i]);
        QListWidgetItem* item = new QListWidgetItem();
        item->setIcon(QIcon(nailImg));
        listWidget->addItem(item);
        // 将全尺寸图片路径与缩略图列表项关联        
        item->setData(Qt::UserRole, imageFiles[i]);
    }
}

void QThumbnailList::setIconMaxNums(int nums)
{
    maxCount_ = nums;
}

void QThumbnailList::ClearAllIcons()
{
    iconImageList_.clear();
    listWidget->clear();
}

void QThumbnailList::showFullSizeImage(QListWidgetItem* item)
{
    QString fullSizePath = item->data(Qt::UserRole).toString();
    int selectIndex = listWidget->currentRow();

    QImage& selImage = iconImageList_.at(selectIndex);
    //selImage.save("d:\\test199.jpg");
    QPixmap showImg = QPixmap::fromImage(selImage);
    QPixmap scaledPixmap = showImg.scaled(
        label_->size(),
        Qt::KeepAspectRatio, // 保持宽高比
        Qt::SmoothTransformation // 平滑缩放
    );
    label_->setPixmap(scaledPixmap);
    label_->show();
    label_->raise();
}
